<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
/**
 * Database search for movie titles and keywords
 */
class SearchController extends Zend_Controller_Action{
    public function indexAction(){
        $this->_redirect( '/search/play/' );
    }

	public function playAction(){
		$request = $this->getRequest();

		if( $request->isXmlHttpRequest() || $request->format == 'json' ){
			$this->_forward('json');
			return;
		}

		$search = new Default_Model_Search();
		$this->view->pageTitle = 'Search the archive';

        if( null !== $request->q || strlen( trim( $request->q ) ) != 0 ){
			$search->setLimit( $request->limit )
					->setOffset( $request->offset );

			$this->view->pageTitle = sprintf( 'Search results for <i>"%s"</i>', trim($request->q));

			$results = $search->search( $request->mode, $request->q );

			if( $request->mode == 'movie' ){
				$this->view->movies = $results;
				//var_dump( $results ); exit;
			}
			else{
				$this->view->movies 	 = $search->getMovies();
				$this->view->channels 	 = $search->getChannels();
				$this->view->annotations = $results;
				$this->view->annotation  = $results[0];
			}
		}

		$this->view->search = $search;
	}

    public function jsonAction(){
        $request  = $this->getRequest();

		$response = (object) array(
			'success'	=> true,
			'movies'		=> array(),
			'channels'		=> array(),
			'annotations'	=> array(),
		);

        if( null === $request->q || strlen( trim( $request->q ) ) == 0 ){
            $response->succes  = false;
            $response->message = "Cannot search an empty query ( q = NULL )";
        }
		else{
			$search = new Default_Model_Search();
            $search->setLimit(0);

			$results = $search->search( $request->mode, $request->q );

			if( $mode == 'movie' ){
				$response->movies      = array_map(
					create_function( '$value', 'return $value->toArray();' ),
					(array) $results
				);
			}
			else{
				$response->movies      = array_map(
					create_function( '$value', 'return $value->toArray();' ),
					(array) $search->getMovies()
				);
				$response->channels     = array_map(
					create_function( '$value', 'return $value->toArray();' ),
					(array) $search->getChannels()
				);
				$response->annotations = array_map(
					create_function( '$value', 'return $value->toArray();' ),
					(array) $results
				);
			}
		}

        $this->_helper->json( $response );
	}
}
